import { Socket } from 'socket.io';

type ClientEventValueFormat = 'json' | 'string'

export class ClientEvent {
    event: string;
    value: string;
    format?: ClientEventValueFormat;

    constructor(event: string, value: string, format: ClientEventValueFormat) {
        this.event = event
        this.value = value
        this.format = format
    }

    // constructor(event: string, value: string, format: ClientEventValueFormat) {
    //     this.event = event
    //     this.value = value
    //     this.format = format
    // }
}

export class SocketClientWrap {
    id: string
    address: string
    client: Socket
    events: ClientEvent[]

    constructor(client: Socket, address: string) {
        this.client = client
        this.address = address
        this.id = client.id
    }

    registerEvents(events: ClientEvent[]){
        this.events = events
    }
}

export default {
    socketClients: [],
    getList(): any[] {
        return this.socketClients;
    },
    saveClientWrap(socketClient: SocketClientWrap) {
        this.socketClients.push(socketClient)
    },
    saveClient(client: Socket) {
        this.socketClients.push(new SocketClientWrap(client, client.handshake.address))
    },
    getById(id: string): SocketClientWrap {
        return this.socketClients.find(item => item.client.id == id)
    },
    removeById(id: string) {
        this.socketClients = this.socketClients.filter(item => item.id != id)
    }
}